package com.demo.listener;
 
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.demo.pojo.ErrorLog;
import com.demo.pojo.WxUser;
import com.demo.service.ErrorLogService;
import com.demo.service.WxUserService;
import com.demo.util.JwtUtils;
import com.demo.util.WeChatTemplateMsgUtils;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 消息消费者
 *
 */
@Component
public class FirstConsumer {

    @Autowired
    private ErrorLogService errorLogService;

    @Autowired
    private  WeChatTemplateMsgUtils weChatTemplateMsgUtils;

    @Autowired
    private WxUserService wxUserService;

    //@RabbitListener(queues = {"first-queue","second-queue"})
    @RabbitListener(queues = {"queue1"})
    public void handleMessage(String message) {
        // 处理消息
        System.out.println("MQ监听消息 {}  :"+message);
        ErrorLog errorLog = JSON.parseObject(message, ErrorLog.class);
        errorLogService.insertSelective(errorLog);

        //进行上报发邮件、短息、微信公众号短信 等
        //查询关注公众号的用户列表
        List<WxUser> listWxUser = wxUserService.getListWxUser();
        if (CollectionUtil.isNotEmpty(listWxUser)){
            for (int i = 0; i <listWxUser.size() ; i++) {
                WxUser wxUser = listWxUser.get(i);
                weChatTemplateMsgUtils.manuscriptExamine(wxUser.getUsername(),errorLog);
            }
        }
    }
}
